package com.qs.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.enums.RoleType;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;
import com.qs.dao.QsAoTestDAO;
import com.qs.enums.PeriodOption;
import com.qs.po.QsAoTest;
import com.qs.util.Period;
import com.qs.util.PeriodUtil;
import com.qs.vo.QsAoTestItem;

public class ApparatusOperateTestService {

	public static QsAoTestItem loadApparatusOperateTest(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			if (StringUtils.isEmpty(itemId))
				return null;
			QsAoTestDAO dao = new QsAoTestDAO();
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<QsAoTestItem> loadApparatusOperateTestList(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			QsAoTestDAO dao = new QsAoTestDAO();
			int type = Integer.parseInt(request.getParameter("testType"));
			List<QsAoTestItem> list = dao.findByType(type);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveApparatusOperateTest(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			QsAoTest bean = new QsAoTest();
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setQsGetScore(Float.valueOf(request.getParameter("qsGetScore")));
			bean.setQsAotDate(request.getParameter("qsAotDate"));
			bean.setQsAotType(Integer.parseInt(request.getParameter("qsAotType")));
			bean.setQsLostScore(Float.valueOf(request.getParameter("qsLostScore")));
			bean.setPersonId(Integer.parseInt(request.getParameter("personId")));
			QsAoTestDAO dao = new QsAoTestDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject modifyApparatusOperateTest(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			QsAoTestDAO dao = new QsAoTestDAO();
			QsAoTest bean = dao.find(QsAoTest.class, itemId);
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setQsGetScore(Float.valueOf(request.getParameter("qsGetScore")));
			bean.setQsAotDate(request.getParameter("qsAotDate"));
			bean.setQsAotType(Integer.parseInt(request.getParameter("qsAotType")));
			bean.setQsLostScore(Float.valueOf(request.getParameter("qsLostScore")));
			bean.setPersonId(Integer.parseInt(request.getParameter("personId")));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static ReturnObject removeApparatusOperateTest(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			QsAoTestDAO dao = new QsAoTestDAO();
			dao.remove(QsAoTest.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static List<QsAoTestItem> search(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			QsAoTestDAO dao = new QsAoTestDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<QsAoTestItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject removeBatchApparatusOperateTest(
			HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			QsAoTestDAO dao = new QsAoTestDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(QsAoTest.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}

	public static int getPageCount(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) {
		try {
			QsAoTestDAO dao = new QsAoTestDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)
					/ PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request,
			HttpServletResponse response, JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String testDate = request.getParameter("testDate");
		String deptId = request.getParameter("deptId");
		String qsAotType = request.getParameter("qsAotType");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", Integer
					.parseInt(deptId));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(qsAotType)) {
			FieldBean field = new FieldBean("qsAotType", Integer
					.parseInt(qsAotType));
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(testDate)) {
			Period period = PeriodUtil.getPeriod(PeriodOption.valueOf(Integer.parseInt(testDate)));
			if(!period.isAll()) {
				searchBean.setBeginTimeName("qsAotDate");
				searchBean.setBeginTimeValue(period.getBegin());
				searchBean.setEndTimeName("qsAotDate");
				searchBean.setEndTimeValue(period.getEnd());
			}
		}
		// 设置权限
		User user = (User) request.getSession().getAttribute(
				Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
